package minio.util;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Part;
import minio.config.MyMinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Component
public class MinioUtil {
    public static final long PART_MAX_SIZE = 5 * 1024 * 1024;

    private static MyMinioClient minioClient;

    public static String BUCKET_NAME;

    public static String URL;

    @Value("${minio.bucketName}")
    private void setBucketName(String bucketName) {
        MinioUtil.BUCKET_NAME = bucketName;
    }

    @Value("${minio.url}")
    private void setUrl(String url) {
        MinioUtil.URL = url;
    }

    @Autowired
    private void setMinioClient(MyMinioClient minioClient) {
        MinioUtil.minioClient = minioClient;
    }




    public static String createUploadId(PutObjectArgs args,Multimap<String, String> headers) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {
        return minioClient.createMultipartUpload(args.bucket(), args.object(), headers).result().uploadId();
    }


    public static Multimap<String, String> getHeaders(PutObjectArgs args) throws IOException {
        Multimap<String, String> headers = args.extraHeaders() != null ? HashMultimap.create(args.extraHeaders()) : HashMultimap.create();
        headers.putAll(args.genHeaders());
        if (!headers.containsKey("Content-Type")) {
            headers.put("Content-Type", args.contentType());
        }
        return headers;
    }

    public static ObjectWriteResponse upload(PutObjectArgs args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return minioClient.putObject(args);
    }


    public static String uploadPart(PutObjectArgs args, BufferedInputStream data, long availableSize, String uploadId, int partNumber, Multimap<String, String> headers) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {
        return minioClient.uploadPart(args.bucket(), args.region(), args.object(), data, (int) availableSize, uploadId, partNumber, headers, null).etag();
    }


    public static void completeMultipartUpload(PutObjectArgs args, String uploadId, Part[] parts) throws ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, IOException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {
        minioClient.completeMultipartUpload(args.bucket(), args.region(), args.object(), uploadId, parts, null, null);
    }






}
